package com.funtonow.jqdk.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.funtonow.jqdk.R;
import com.funtonow.jqdk.adapter.StringAdapter;
import com.funtonow.jqdk.base.BaseActivity;
import com.funtonow.jqdk.utils.FileUtils;

import java.util.List;

import butterknife.BindView;

/**
 * @author Jiajien
 * @date 2019/5/24
 */
public class SelectStringActivity extends BaseActivity {

    @BindView(R.id.tv_select_title)
    TextView tvSelectTitle;
    @BindView(R.id.rlv_select_data)
    RecyclerView recyclerView;
    private List<String> dataStringBeanList;
    @BindView(R.id.ll_height)
    LinearLayout llHeight;

    private StringBuilder bust = new StringBuilder(); // 胸围
    private JSONArray jsonArray;
    private StringAdapter stringAdapter;

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) recyclerView.getLayoutParams(); //取控件textView当前的布局参数 linearParams.height = 20;// 控件的高强制设成20
        linearParams.width = WindowManager.LayoutParams.MATCH_PARENT;// 控件的宽强制设成30
        if (llHeight.getMeasuredHeight() > (int) getResources().getDimension(R.dimen.x300)) {
            linearParams.height = (int) getResources().getDimension(R.dimen.x300);
        } else {
            linearParams.height = WindowManager.LayoutParams.WRAP_CONTENT;// 控件的宽强制设成30
        }
        llHeight.setLayoutParams(linearParams); //使设置好的布局参数应用到控件
    }


    @Override
    public int getLayoutRes() {
        return R.layout.activity_select_string;
    }

    @Override
    public void onCreateActivity(@Nullable Bundle savedInstanceState) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        getWindow().setAttributes(lp);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        //设置分隔线
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        //设置增加或删除条目的动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        initView();
    }

    private void initView() {
        String dataJson = FileUtils.getJson(this, "baseData.json");
        jsonArray = JSON.parseObject(dataJson).getJSONArray(getIntent().getStringExtra("key"));
        tvSelectTitle.setText(getIntent().getStringExtra("title"));
        dataStringBeanList = JSON.parseArray(jsonArray.toJSONString(), String.class);
        stringAdapter = new StringAdapter(dataStringBeanList);
        recyclerView.setAdapter(stringAdapter);
        stringAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent();
                intent.putExtra("result", stringAdapter.getData().get(position));
                setResult(RESULT_OK, intent);
                finish();
            }
        });

    }

}
